package com.canbot.u05.activity.presidemode.newcode;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.newcode.adapter.NChoiceActionAdapter;
import com.canbot.u05.activity.presidemode.newcode.bean.ActionBean;
import com.canbot.u05.activity.presidemode.newcode.bean.NModuleType;
import com.canbot.u05.activity.presidemode.newcode.bean.SpacesItemDecoration;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IHeadCommonClick;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IItemDragClick;
import com.canbot.u05.activity.presidemode.newcode.view.HeadCommonView;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 选择指定的动作
 */
public class NChoiceActionActivity extends BaseHostActivity {
    private static final String TAG = "NChoiceActionActivity";
    private List<ActionBean> armActionList = new ArrayList<>();
    private List<ActionBean> headActionList = new ArrayList<>();
    private List<ActionBean> choiceActionList = new ArrayList<>();
    @BindView(R.id.headCommonView)
    HeadCommonView headCommonView;
    @BindView(R.id.recycleView_aciton_list)
    RecyclerView recycleViewAcitonList;
    @BindView(R.id.recycleView_aciton_choiced)
    RecyclerView recycleViewAcitonChoiced;
    @BindView(R.id.btnhead)
    Button btnhead;
    @BindView(R.id.btnArm)
    Button btnArm;
    @BindView(R.id.delete)
    ImageButton delete;
    private NChoiceActionAdapter nActionExistAdapter;
    private NChoiceActionAdapter nActionChoiceAdapter;
    private ItemTouchHelper itemTouchHelper;

    @OnClick({R.id.btnArm, R.id.btnhead})
    public void btnClick(View view) {
        switch (view.getId()) {
            case R.id.btnArm:
                boolean selected = btnArm.isSelected();
                if (!selected) {
                    btnArm.setSelected(true);
                    btnhead.setSelected(false);
                    nActionExistAdapter.update(armActionList, false);
                }
                break;
            case R.id.btnhead:
                selected = btnhead.isSelected();
                if (!selected) {
                    btnArm.setSelected(false);
                    btnhead.setSelected(true);
                    nActionExistAdapter.update(headActionList, true);
                }
                break;
        }
    }

    @Override
    protected void initEventData() {
        //MapVoiceAndEyes.playMapVoiceAndEyes(MapVoiceAndEyes.voiceEyesList.get(22));
        initCommonHead();
        initIntent();
        recycleViewAcitonList.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        recycleViewAcitonChoiced.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        armActionList = armActionGet();
        headActionList = headActionGet();
        nActionExistAdapter = new NChoiceActionAdapter(this, false, headActionList);
        nActionChoiceAdapter = new NChoiceActionAdapter(this, true, choiceActionList);
        nActionChoiceAdapter.setiItemClick(new IItemDragClick() {
            @Override
            public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
                itemTouchHelper.startDrag(viewHolder);
            }

            @Override
            public void onClick(int position, View v) {

            }
        });
        nActionExistAdapter.setiItemClick(new IItemDragClick() {
            @Override
            public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
            }

            @Override
            public void onClick(int position, View v) {
                if (choiceActionList.size() < 30) {
                    ActionBean actionBean;
                    if (nActionExistAdapter.isHeadActionShow) {
                        actionBean = headActionList.get(position);
                    } else {
                        actionBean = armActionList.get(position);
                    }
                    choiceActionList.add(actionBean);
                    nActionChoiceAdapter.notifyItemInserted(choiceActionList.size());
                    recycleViewAcitonChoiced.scrollToPosition(choiceActionList.size() - 1);
                } else {
                    Toast.makeText(getApplicationContext(), getString(R.string.action_too_length), Toast.LENGTH_SHORT).show();
                }
            }
        });
        btnhead.setSelected(true);
        recycleViewAcitonChoiced.setAdapter(nActionChoiceAdapter);
        SpacesItemDecoration spacesItemDecoration = new SpacesItemDecoration(20);
        recycleViewAcitonChoiced.addItemDecoration(spacesItemDecoration);
        recycleViewAcitonList.addItemDecoration(spacesItemDecoration);
        recycleViewAcitonList.setAdapter(nActionExistAdapter);
        initItepHelp();
    }

    private void initIntent() {
        List<ActionBean> list = (List<ActionBean>) getIntent().getSerializableExtra(DATA);
        if (list != null) {
            choiceActionList.addAll(list);
        }
    }

    private void initItepHelp() {
        itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
            private boolean isDelete;
            private RecyclerView.ViewHolder mViewHolder;

            @Override
            public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
                int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
                int swipeFlags = 0;
                return makeMovementFlags(dragFlags, swipeFlags);
            }

            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                int fromPos = viewHolder.getAdapterPosition();
                int toPos = target.getAdapterPosition();
                Collections.swap(choiceActionList, fromPos, toPos);
                nActionChoiceAdapter.notifyItemMoved(fromPos, toPos);
                return true;
            }

            @Override
            public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
                //拖拽的时候
                if (actionState == ItemTouchHelper.ACTION_STATE_IDLE) {
                    isDelete = true;
                    int width = delete.getWidth();
                    int height = delete.getHeight();
                    int[] ints = new int[2];
                    int[] ints2 = new int[2];
                    delete.getLocationOnScreen(ints);
                    int w = mViewHolder.itemView.getWidth();
                    int h = mViewHolder.itemView.getHeight();
                    mViewHolder.itemView.getLocationOnScreen(ints2);
                    Rect rect = new Rect(ints2[0], ints2[1], ints2[0] + w, ints2[1] + h);
                    boolean contains = rect.contains(ints[0] + width / 2, ints[1] + height / 2);
                    if (contains) {
                        int adapterPosition = mViewHolder.getAdapterPosition();
                        choiceActionList.remove(adapterPosition);
                        itemTouchHelper.onChildViewDetachedFromWindow(mViewHolder.itemView);
                        nActionChoiceAdapter.notifyItemRemoved(adapterPosition);
                    } else {
                        isDelete = false;
                    }
                } else {
                    isDelete = false;
                    mViewHolder = viewHolder;
                }
                super.onSelectedChanged(viewHolder, actionState);
            }

            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {

            }

            /**
             * 长按拖长按拖拽删除 true  自己处理 false 调用 itemTouchHelper.startDrag(holder);
             */
            @Override
            public boolean isLongPressDragEnabled() {
                return false;
            }

            /**
             * 滑动删除
             *
             * @return
             */
            @Override
            public boolean isItemViewSwipeEnabled() {
                return false;
            }

            @Override
            public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
                if (isDelete) {
                    return;
                }
                updateDragInfo(viewHolder, actionState);
                super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
            }

            @Override
            public void onChildDrawOver(@NonNull Canvas c, @NonNull RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
                if (isDelete) {
                    return;
                }
                updateDragInfo(viewHolder, actionState);
                super.onChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
            }

            @Override
            public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
                super.clearView(recyclerView, viewHolder);
                recyclerView.post(() -> {
                    delete.setSelected(false);
                    nActionChoiceAdapter.notifyDataSetChanged();
                });
            }
        });
        itemTouchHelper.attachToRecyclerView(recycleViewAcitonChoiced);
    }

    private void updateDragInfo(@NonNull RecyclerView.ViewHolder viewHolder, int actionState) {
        if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
            int width = delete.getWidth();
            int height = delete.getHeight();
            int[] delelteViewPos = new int[2];
            int[] itemViewPos = new int[2];
            delete.getLocationOnScreen(delelteViewPos);
            int w = viewHolder.itemView.getWidth();
            int h = viewHolder.itemView.getHeight();
            viewHolder.itemView.getLocationOnScreen(itemViewPos);
            Rect rect = new Rect(itemViewPos[0], itemViewPos[1], itemViewPos[0] + w, itemViewPos[1] + h);
            boolean contains = rect.contains(delelteViewPos[0] + width / 2, delelteViewPos[1] + height / 2);
            delete.setSelected(contains);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        MapVoiceAndEyes.stop();
    }

    private void initCommonHead() {
        headCommonView.setiHeadCommonClick(pos -> {
            switch (pos) {
                case IHeadCommonClick.LEFT:
                    finish();
                    break;
                case IHeadCommonClick.RIGHT:
                    Intent intent = new Intent();
                    intent.putExtra(DATA, (Serializable) choiceActionList);
                    setResult(NModuleType.ACTION_EXIST, intent);
                    finish();
                    break;
            }
        });
    }

    private List<ActionBean> headActionGet() {
        ArrayList<ActionBean> nActionBeans = new ArrayList<>();
        nActionBeans.add(new ActionBean("10201", "看右边", "2.6"));
        nActionBeans.add(new ActionBean("10202", "看左边", "2.6"));
        nActionBeans.add(new ActionBean("10203", "点头", "2.9"));
        nActionBeans.add(new ActionBean("10204", "摇头", "3.1"));
        nActionBeans.add(new ActionBean("14001", "抬头", "4"));
        nActionBeans.add(new ActionBean("14002", "低头", "4"));
        nActionBeans.add(new ActionBean("11501", "看左上角", "4"));
        nActionBeans.add(new ActionBean("11801", "看右上角", "4"));
        nActionBeans.add(new ActionBean("14010", "仰头大笑", "3.9"));
        nActionBeans.add(new ActionBean("14008", "左顾右盼", "4.5"));
        nActionBeans.add(new ActionBean("-1", "空动作", "1"));
        return nActionBeans;
    }

    private List<ActionBean> armActionGet() {
        ArrayList<ActionBean> nActionBeans = new ArrayList<>();
        nActionBeans.add(new ActionBean("10101", "述说", "4.8"));
        nActionBeans.add(new ActionBean("10104", "双手上下晃动", "4"));
        nActionBeans.add(new ActionBean("10107", "张开双臂", "3.7"));
        nActionBeans.add(new ActionBean("10109", "请看右边", "3.2"));
        nActionBeans.add(new ActionBean("10111", "请看左边", "4.1"));
        nActionBeans.add(new ActionBean("10110", "请看右上角", "4"));
        nActionBeans.add(new ActionBean("10112", "请看左上角", "4"));
        nActionBeans.add(new ActionBean("10113", "右手曲臂", "3"));
        nActionBeans.add(new ActionBean("10205", "敬礼", "4.4"));
        nActionBeans.add(new ActionBean("10228", "握手", "8"));
        nActionBeans.add(new ActionBean("10302", "打招呼", "5"));
        nActionBeans.add(new ActionBean("10502", "右手数数1", "3.9"));
        nActionBeans.add(new ActionBean("10503", "右手数数2", "3.9"));
        nActionBeans.add(new ActionBean("10504", "右手数数3", "4.4"));
        nActionBeans.add(new ActionBean("10505", "右手数数4", "5.3"));
        nActionBeans.add(new ActionBean("11701", "飞吻", "3.6"));
        nActionBeans.add(new ActionBean("12101", "摇头摆手", "4"));
        nActionBeans.add(new ActionBean("12301", "叉腰", "4.1"));
        nActionBeans.add(new ActionBean("10507", "耍帅", "3"));
        nActionBeans.add(new ActionBean("13101", "石头", "7"));
        nActionBeans.add(new ActionBean("13102", "剪刀", "6.5"));
        nActionBeans.add(new ActionBean("13103", "布", "7"));
        return nActionBeans;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_naction_choice;
    }
}
